const R = require('ramda')
const { Op } = require('sequelize')

const genWhereObj = R.curry((likeFields = [], data = {}) => {
  return Object.keys(data).reduce((acc, key) => {
    const value = data[key]
    if (!value) return acc
    if (likeFields.includes(key))
      return { ...acc, [key]: { [Op.like]: `%${value}%` } }
    return { ...acc, [key]: value }
  }, {})
})

const getAddAndRemoveIdList = (oldIds = [], newIds = []) => {
  const deleteIds = R.difference(oldIds, newIds)
  const addIds = R.difference(newIds, oldIds)
  return {
    deleteIds,
    addIds,
  }
}

module.exports = {
  genWhereObj,
  getAddAndRemoveIdList,
}
